import { defineStore } from 'pinia'
import MsgServe from '@/api/msg'
import type { MsgItem, MsgStoreState } from '../type'

const useMsgStore = defineStore('msg', {
  state: (): MsgStoreState => {
    return {
      receiveList: [],
      sendList: [],
      currentItem: {} as MsgItem
    }
  },

  getters: {},

  actions: {
    /**
     * 获取消息列表
     */
    async fetchMessageList() {
      try {
        const resp = await MsgServe.getMessageList()
        const data = resp.data || {}
        this.receiveList = data.receive || []
        this.sendList = data.send || []
      } catch (error) {
        console.error('获取消息列表失败：', error)
      }
    },
    setCurrentItem(msgItem: MsgItem) {
      this.currentItem = msgItem
    }
  }
})

export { useMsgStore }
